package com.ruoyi.web.controller.word;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.word.service.ICommonRaService;
import com.ruoyi.word.service.ICommonWordTpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/word")
public class CommonWordApiController extends BaseController {

    @Autowired
    private ICommonRaService biz;

    @Autowired
    private ICommonWordTpService tpService;

    @PostMapping("/buildRa")
    public R buildRa(){
        try {
            return R.data(biz.buildRa());
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail();
    }

    @PostMapping("/saveFromBaidu")
    public R saveFromBaidu() {
        try {
            saveFromBaiduTask();
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail();
    }

    private void saveFromBaiduTask(){
        try {
            tpService.dealFromBaidu();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                Thread.sleep(1000*60*3);
                // 循环调用
                saveFromBaiduTask();
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        }
    }

    @PostMapping("/saveFromYoudao")
    public R saveFromYoudao(){
        try {
            return R.data(tpService.dealFromYoudao());
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail();
    }

}
